﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CommerceOne.Domains.Entities;
using CommerceOne.Domains.Repositories;
using CommerceOne.Domains.SqlRepositories;
using CommerceOne.Domains.UnBoundEnities;
using CommerceOne.Models;
using Cart = CommerceOne.Domains.UnBoundEnities.Cart;

namespace CommerceOne.Controllers
{
    public class CartController : ControllerBase
    {
        private readonly IProductRepository _productRepository = new SqlProductRepository();
        private readonly ICountryRepository _countryRepository = new SqlCountryRepository();
        private readonly ICartRepository _cartRepository = new SqlCartRepository();
        private readonly IUserRepository _userRepository = new SqlUserRepository();

        /// <summary>
        /// Indexes the specified cart.
        /// </summary>
        /// <param name="cart">The cart.</param>
        /// <returns></returns>
        public ActionResult Index(Cart cart)
        {
            string returnUrl = Request.QueryString["returnUrl"].ToString();
            CartViewModel cartViewModel = new CartViewModel();

            this.CommonInitialize(cartViewModel);

            cartViewModel.ReturnUrl = returnUrl;
            cartViewModel.Cart = cart;

            return View(cartViewModel);
        }

        /// <summary>
        /// Adds to cart.
        /// </summary>
        /// <param name="cart">The cart.</param>
        /// <param name="id">The id.</param>
        /// <param name="returnUrl">The return URL.</param>
        /// <returns></returns>
        public RedirectToRouteResult AddToCart(Cart cart, int id, string returnUrl)
        {
            Product product = this._productRepository.GetProduct(id);
            //if (product != null) this.GetCart().Add(product, 1);
            if (product != null) cart.Add(product, 1);

           return RedirectToAction("Index", new { returnUrl = returnUrl });
        }

        /// <summary>
        /// Removes the specified cart.
        /// </summary>
        /// <param name="cart">The cart.</param>
        /// <param name="id">The id.</param>
        /// <param name="returnUrl">The return URL.</param>
        /// <returns></returns>
        public RedirectToRouteResult Remove(Cart cart, int id, string returnUrl)
        {
            Product product = this._productRepository.GetProduct(id);
            if (product != null) cart.Remove(product);
            return RedirectToAction("Index", new { returnUrl });
        }

        /// <summary>
        /// Summeries the specified cart.
        /// </summary>
        /// <param name="cart">The cart.</param>
        /// <returns></returns>
        public PartialViewResult Summery(Cart cart)
        {
            return PartialView(cart);
        }

        /// <summary>
        /// Checks the out.
        /// </summary>
        /// <returns></returns>
        public ActionResult CheckOut()
        {
            CheckOutViewModel viewModel = new CheckOutViewModel();

            this.CommonInitialize(viewModel);

            var user =  this._userRepository.GetUser(5);
            viewModel.Cart = new Domains.Entities.Cart();
            viewModel.Cart.Phone1 = user.Phone1;
            viewModel.Cart.Phone2 = user.Phone2;
            viewModel.Cart.ReceiptName = user.Name + " " + user.LastName;
            viewModel.Cart.ShippingAddress = user.Address;
            viewModel.Cart.ShippingCity = user.City;
            viewModel.Cart.ShippingCountry = user.Country.ToString();
            viewModel.Cart.CellPhone = user.CellPhone;

            // Get Country
            viewModel.Countries = this._countryRepository.All();

            return View(viewModel);
        }

        [HttpPost]
        public ActionResult CheckOut(Cart cart, CheckOutViewModel viewModel)
        {
            Domains.Entities.Cart shippingCart = viewModel.Cart;
            shippingCart.UserId = 5;
            shippingCart.Status = "N";
            shippingCart.CheckoutDate = DateTime.Now;
            shippingCart.CartDetails = new List<CartDetail>();

            foreach (var product in cart.ProductLine)
            {
                CartDetail cartDetail = new CartDetail();
                cartDetail.Price = product.Price;
                cartDetail.ProductId = product.Id;
                cartDetail.ProductName = product.Name;
                cartDetail.Vol = product.Quantity;
                cartDetail.Total = product.SubTotal;
                shippingCart.CartDetails.Add(cartDetail);
            }

            int result = this._cartRepository.Add(shippingCart);

            if (result > 0) 
            {
                cart.ClearCart();
                return RedirectToAction("CheckOutThankYou");
            }

            return View(viewModel);
        }

        public ActionResult CheckOutThankYou()
        {
            CheckOutThankYouViewModel viewModel = new CheckOutThankYouViewModel();
            this.CommonInitialize(viewModel);
            return View(viewModel);
        }
    }
}
